<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<uni-segmented-control 
			:current="current" 
			:values="items" 
			:style-type="styleType" 
			:active-color="activeColor" 
			@clickItem="onClickItem" />
		</view>
		<view class="content">
			<view v-if="current === 0">
				<uni-list style="width:95%">
					<uni-list-chat v-for="item in listData"
					:key="item.id" 
					:avatar="oss_domain + item.dry_bill_ext[0].dry_object"
					:title="item.dry_title" 
					:note="(item.dry_apply == user.id ? ('团队:' + item.dry_team_name) : ('申请人:' + item.dry_apply_note)) + ' ' + (item.dry_reim_status == 1 ? '状态: 已完成' : '状态: 处理中')" 
					:time="item.dry_add_time" 
					:clickable="true" 
					:badge-text="item.dry_audit_submit == 1 ? (item.dry_cost + '元') : '审核未通过，重新填写'" 
					@click="goDetail(item)" />
				</uni-list>
			</view>
			<view v-if="current === 1">
				<uni-list style="width:95%" >
					<uni-list-chat v-for="item in auditList"
					:key="item.id" 
					:avatar="oss_domain + item.dry_bill_ext[0].dry_object"
					:title="item.dry_title" 
					:note="(item.dry_apply == user.id ? ('团队:' + item.dry_team_name) : ('申请人:' + item.dry_apply_note)) + ' ' + (item.dry_reim_status == 1 ? '状态: 已完成' : '状态: 处理中')" 
					:time="item.dry_add_time" 
					:clickable="true" 
					:badge-text="item.dry_cost + '元'" 
					@click="goDetail(item)" />
				</uni-list>
			</view>
			<view v-if="current === 2">
				<uni-list style="width:95%">
					<uni-list-chat v-for="item in reimList"
					:key="item.id" 
					:avatar="oss_domain + item.dry_bill_ext[0].dry_object"
					:title="item.dry_title" 
					:note="(item.dry_apply == user.id ? ('团队:' + item.dry_team_name) : ('申请人:' + item.dry_apply_note)) + ' ' + (item.dry_reim_status == 1 ? '状态: 已完成' : '状态: 处理中')" 
					:time="item.dry_add_time" 
					:clickable="true" 
					:badge-text="item.dry_cost + '元'" 
					@click="goDetail(item)" />
				</uni-list>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listData: [],
				auditList: [],
				reimList: [],
				teams: [],
				employee: [],
				oss_domain: '',
				user: [],
				items: ['所有', '审核', '核销'],
				current: 0,
				activeColor: '#007aff',
				styleType: 'text'
			}
		},
		onLoad(){
			this.user = this.$dry.get("user");
			this.oss_domain = this.$dry.get("oss_domain");
		},
		onShow(){
			this.getReimbursementList();
			
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex;
					if(e.currentIndex === 0){
						this.getReimbursementList();
					}
					if(e.currentIndex === 1){
						this.getAuditList();
					}
					if(e.currentIndex === 2){
						this.getReimList();
					}
				}
			},
			getReimbursementList(){
				var token = this.$dry.getToken();
				var data = [];
				data['page'] = this.page;
				if(token){
				    data = this.$request.run("getReimbursementList",data,token);
				    uni.request({
				    	url: data[0].url,
				    	method: data[0].method,
				    	header: data[0].header,
				    	data: data[0].data,
				    	success: data => {
							var list = data.data.data;
							this.listData = list.list;
							this.teams = list.teams;
							this.employee = list.employee
						}
					})
				}
			},
			getAuditList(){
				var newItems = [];
				this.listData.forEach(e => {
					newItems.push(e);
				})
				var auditList = [];
				for(var i = 0; i< this.teams.length; i++){
					if(this.teams[i].dry_user== this.user.id && this.teams[i].dry_is_open_control == 0){
						for(var j = 0; j < newItems.length; j++){
							if(this.teams[i].id == newItems[j].dry_team){
								auditList.push(newItems[i]);
								newItems.splice(i,1);
							}
						}
					}
				}
				if(this.employee.length != 0){
					this.employee.forEach(e => {
						if(e.dry_is_auditor == 1){
							for(var i = 0; i < newItems.length; i++){
								if(newItems[i].dry_team == e.dry_team && newItems[i].dry_audit_step == Number(e.dry_audit_step) - 1){
									auditList.push(l);
									newItems.splice(i,1);
								}
							}
						}
					})
				}
				this.auditList = auditList;
			},
			getReimList(){
				var reimList = [];
				this.teams.forEach(t => {
					if(t.dry_reim_verify == this.user.id){
						this.listData.forEach(l => {
							if(l.dry_team == t.id && l.dry_audit_step == l.dry_team_step){
								reimList.push(l);
							}
						})
					}
				})
				this.reimList = reimList;
			},
			goDetail(item){
				this.$dry.set("reim_data",item);
				if(item.dry_audit_submit == 0 && item.dry_apply == this.user.id){
					uni.navigateTo({
						url: '../new/reimbursement-new-edit'
					})
				}
				else{
					uni.navigateTo({
						url: 'reim-detail'
					})
				}
			}
		}
	}
</script>

<style>

</style>
